//
//  PersonChangePassWordView.m
//  Inforcollection
//
//  Created by macbook on 16/1/12.
//  Copyright © 2016年 oceansoft.liw. All rights reserved.
//

#import "PersonChangePassWordView.h"

@implementation PersonChangePassWordView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self creatAllView];

    }
    return self;
}
- (void)creatAllView
{
    array = @[@"新密码",@"确认密码",@"手机号码",@"验证码"];
    arr = [array subarrayWithRange:NSMakeRange(0, 3)];
    [self creatTableViews];
}

- (void)creatTableViews
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.scrollEnabled = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [[UITableViewHeaderFooterView appearance] setTintColor:KTableViewBackColor];
    _tableView.backgroundColor = KTableViewBackColor;

    [self addSubview:_tableView];
}

#pragma mark -UITableViewDataSource  &  UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }else if (section == 1){
        return 1;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    PersonInfoTableViewCell *cell = [PersonInfoTableViewCell changeInfoWithTableView:tableView];
    
    if (indexPath.section == 0) {
       
        cell.nameLabel.text = arr[indexPath.row];
        if (indexPath.row == 0)
        {
            cell.infoTextField.secureTextEntry = YES;
            
            _passwordField = cell.infoTextField;
            
        }else if (indexPath.row == 1)
        {
            cell.infoTextField.secureTextEntry = YES;

            _newPasswordField = cell.infoTextField;
            
        }else if (indexPath.row == 2)
        {
            PhoneYZMTableViewCell *cell = [PhoneYZMTableViewCell phoneYZMGetWithTableView:tableView];
            _phoneNumbersField = cell.YZMTextField;
            [_phoneNumbersField addTarget:self action:@selector(TextFieldDidChanges:) forControlEvents:UIControlEventEditingChanged];
            cell.nameLabel.text = arr[indexPath.row];
            
            _YZMField = cell.YZMTextField;
            
            cell.YZMLabel.tag = UserTag + 3;
            
            [cell touchLabelGetYZM1];
            
            return cell;
        }
            
    }else if (indexPath.section == 1)
    {
        cell.nameLabel.text = [array lastObject];
        
        _YZMField = cell.infoTextField;
        
        [_YZMField addTarget:self action:@selector(TextFieldDidChanges:) forControlEvents:UIControlEventEditingChanged];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
#pragma mark -UITextField输入条件限制
- (void)TextFieldDidChanges:(UITextField *)textField
{
    if (textField == _phoneNumbersField) {
        if (textField.text.length > 11) {
            textField.text = [textField.text substringToIndex:11];
        }
    }
    if (textField == _YZMField)
    {
        if (textField.text.length > 6) {
            textField.text = [textField.text substringToIndex:6];
        }
    }
}
#pragma mark -UITableView的headerView与FooterView
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 5;
    }else if (section == 1){
        return 10;
    }
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        UIView *headerView = [[UIView alloc] init];
        headerView.backgroundColor = KTableViewBackColor;
        
        UIButton *saveBtn = [UIButton buttonWithType:0];
        saveBtn.frame = CGRectMake(10, 10, tableView.frame.size.width-20, 40);
        saveBtn.backgroundColor = KButtonBackColor;
        saveBtn.layer.cornerRadius = 5.0f;
        [saveBtn setTitle:@"保存修改" forState:UIControlStateNormal];
        [saveBtn addTarget:self action:@selector(saveChangeInPage2) forControlEvents:UIControlEventTouchUpInside];
        saveBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        [headerView addSubview:saveBtn];
        return headerView;

    }
    return nil;
}

@end
